﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

namespace MicroTest
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                // 处理未捕获的异常
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常   
                Application.ThreadException += Application_ThreadException;
                //处理非UI线程异常   
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                bool bCreateNew;
#if DEBUG
                using (Mutex mu = new Mutex(true, "mt_client_test", out bCreateNew))
#else
                using (Mutex mu = new Mutex(true, "mt_client", out bCreateNew))
#endif
                {
                    if (bCreateNew)
                    {
                        ELogger.Trace(string.Format("程序启动! v{0}", Application.ProductVersion));
                        //LoadStudentBindList();
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        MTInfo = new MicroTestInfo();
                        CurrentClassInfo = new ClassInfo();
                        Application.Run(new TeacherViewer());
                        {
                            FrmStartNewExt fs = new FrmStartNewExt();
                            MTInfo = new MicroTestInfo();
                            CurrentClassInfo = new ClassInfo();
                            if (DialogResult.OK == fs.ShowDialog())
                            {
                                SceneType st = fs.EnterSceneType;
                                fs.Dispose();
                                //Application.Run(new FrmSwhich(st));
                                Application.Run(new TeacherViewer());
                            }
                        }
                    }
                    else
                        ELogger.Trace("已经有一个示例在运行，本次启动退出");
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show("系统出现异常：" + (ex.Message + " " + (ex.InnerException != null && ex.InnerException.Message != null && ex.Message != ex.InnerException.Message ? ex.InnerException.Message : "")) + ",请重启程序。");
                ELogger.Error("系统出现异常 Message: " + ex.Message);
                ELogger.Error("系统出现异常 StatckTrace: " + ex.StackTrace);
                ELogger.Error("系统出现异常 Source: " + ex.Source);

            }


        }
        
        
        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            var ex = e.Exception;
            if (ex != null)
            {
                ELogger.Error("Application_ThreadException Message: "+ex.Message);
                ELogger.Error("Application_ThreadException StatckTrace: " + ex.StackTrace);
                ELogger.Error("Application_ThreadException Source: " + ex.Source);
                if (FrmSwhich.SwhichFrm.GetSceneType() == SceneType.Scene_Class)
                {
                    try
                    {
                        FrmSwhich.SwhichFrm.SaveDataWhenExcetionAccurs();
                        string dataFile = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\WiseClass\\class.dat";
                        SerializeHelper.SerialData(dataFile, Program.CurrentClassInfo);
                        ELogger.Trace("保存上课数据");
                    }
                    catch (Exception exc)
                    {
                        ELogger.Trace("保存上课数据失败");

                    }
                }
            }

            MessageBox.Show("系统出现异常：" + (ex.Message + " " + (ex.InnerException != null && ex.InnerException.Message != null && ex.Message != ex.InnerException.Message ? ex.InnerException.Message : "")));
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var ex = e.ExceptionObject as Exception;
            if (ex != null)
            {
                ELogger.Error("CurrentDomain_UnhandledException Message: " + ex.Message);
                ELogger.Error("CurrentDomain_UnhandledException StatckTrace: " + ex.StackTrace);
                ELogger.Error("CurrentDomain_UnhandledException Source: " + ex.Source);
                if (FrmSwhich.SwhichFrm.GetSceneType() == SceneType.Scene_Class)
                {
                    try
                    {
                        FrmSwhich.SwhichFrm.SaveDataWhenExcetionAccurs();

                        string dataFile = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\WiseClass\\class.dat";
                        SerializeHelper.SerialData(dataFile, Program.CurrentClassInfo);
                        ELogger.Trace("保存上课数据");
                    }
                    catch (Exception exc)
                    {
                        ELogger.Trace("保存上课数据失败");

                    }
                }
                    
            }

            MessageBox.Show("系统出现异常：" + (ex.Message + " " + (ex.InnerException != null && ex.InnerException.Message != null && ex.Message != ex.InnerException.Message ? ex.InnerException.Message : "")));
        }

        /// <summary>
        /// 微测ID
        /// </summary>
        public static string TEST_KEY;
        /// <summary>
        /// 数据存储目录
        /// </summary>
        public static string DATA_FOLDER;
        /// <summary>
        /// 数据存储根目录
        /// </summary>
        public static string DATA_ROOT;
        /// <summary>
        /// 微测信息
        /// </summary>
        public static MicroTestInfo MTInfo;
        public static StudentInfo StudentInfo=new StudentInfo();
        public static ClassInfo CurrentClassInfo;
        public static bool RemoteClass = true;
    }
}
